import {
  getLoginInformation,
  getVerificationCode,
  userLogin
} from '../../api/user'
import Toast from 'vant-weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber: '18479310875', // 输入的手机号码
    verificationCode: '123456', // 输入的验证码
    phoneErrText: '', // 手机号码输入错误提示
    verificationErrotText: '', // 验证码输入错误提示
    isDisabled: false, // 是否禁用发送验证码按钮
    num: 60,
    text: '获取验证码'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getTitle1();
    return;
    wx.login({
      async success(res) {
        if (res.code) {
          let obj = await getLoginInformation({
            code: res.code
          });
          if (obj.code == 400) {
            Toast.fail(obj.message);
          } else if (obj.code === 401 || obj.code === 200) {
            let {
              openId,
              sessionKey
            } = obj.data;
            that.setData({
              openId: openId,
              sessionKey: sessionKey
            });
            // 缓存openId和sessionKey
            this.setStorageSync("openId",openId);
            this.setStorageSync("sessionKey",sessionKey);
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  // 判断输入的内容是否错误
  isError(e) {
    let {
      dataset
    } = e.target;
    let type = dataset.type;
    let value = e.detail.value.trim();
    this.setErrorText(type, value);
  },
  // 动态设置发送验证码按钮是否可以点击
  enter(e) {
    let phoneNumber = this.data.phoneNumber.trim();
    let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    if (!reg_tel.test(phoneNumber)) {
      this.setData({
        isDisabled: true
      })
    } else {
      this.setData({
        isDisabled: false
      })
    }
  },
  // 获取错误提示
  setErrorText(type, value) {
    let errorText;
    if (type === 'phoneNumber') {
      let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (value.length <= 0) {
        errorText = '手机号码不能为空';
      } else {
        if (!reg_tel.test(value)) {
          errorText = '手机号码格式错误';
        } else {
          errorText = '';
        }
      }
      this.setData({
        phoneErrText: errorText
      });
    } else if (type === 'verificationCode') {
      if (value.length <= 0) {
        errorText = '验证码不能为空';
      } else {
        if (value.length === 6) {
          errorText = '';
        } else {
          errorText = '验证码格式错误';
        }
      }
      this.setData({
        verificationErrotText: errorText
      });
    }
    return errorText;
  },
  // 获取手机号码
  getPhoneNumber(data){
    console.log(data);
  },
  // 发送手机验证码
  async send() {
    let phoneNumber = this.data.phoneNumber.trim();
    let err1 = this.setErrorText('phoneNumber', phoneNumber);
    if (err1 === '') {
      console.log("发送验证码");
      // let obj = await getVerificationCode(phoneNumber);
      if(true){
        this.setData({
          isDisabled: true,
          text: (this.data.num) + '秒'
        });
        let time = setInterval(() => {
          if (this.data.num <= 0) {
            this.setData({
              isDisabled: false,
              text: '获取验证码',
              num: 60
            });
            clearInterval(time)
            return;
          }
          this.setData({
            num: this.data.num - 1,
            text: (this.data.num - 1) + '秒'
          })
        }, 1000)
      }else{
        Toast.fail(obj.message);
      }
    }
  },
  // 登录
  async login() {
    wx.reLaunch({
      url: '/pages/index/index',
    })
    console.log('dddd')
    return
    let phoneNumber = this.data.phoneNumber.trim();
    let verificationCode = this.data.verificationCode.trim();
    let err1 = this.setErrorText('phoneNumber', phoneNumber);
    let err2 = this.setErrorText('verificationCode', verificationCode);
    let that = this;
    if (err1 == '' && err2 == '') {
      let obj = await userLogin({
        code: verificationCode,
        openId: that.data.openId,
        phone: phoneNumber,
        salt: that.data.sessionKey
      });
      if(obj.code === 200){
        this.setStorageSync("token",obj.data.token);
        wx.reLaunch({
          url: '/pages/index/index',
        })
      }else{
        Toast.fail(obj.message);
      }
    } else {
      console.log('失败');
    }
  }
})